package helexis.game.tyrian.domain;

import helexis.game.tyrian.Tyrian;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.Json.Serializable;
import com.badlogic.gdx.utils.OrderedMap;

/**
 * 캐릭터 - 우주선
 * 
 * @author 조만희
 *
 */
public class Ship implements Serializable {
	
	/**
	 * 우주선 모델
	 */
	private ShipModel shipModel;
	/**
	 * 우주선 전방 총
	 */
	private FrontGun frontGun;
	/**
	 * 우주선 쉴드
	 */
	private Shield shield;
	
	
	public Ship() {
	}//end of constructor()
	

	public ShipModel getShipModel() {
		return shipModel;
	}

	public FrontGun getFrontGun() {
		return frontGun;
	}

	public Shield getShield() {
		return shield;
	}
	
	
	public boolean contains(Item item) {
		if (item == null) {
			return false;
		}//end if
		
		return (item == this.shipModel || item == this.frontGun || item == this.shield);
	}//end of contains()
	
	
	/**
	 * 주어진 아이템을 캐릭터 - 우주선 - 에 장착합니다.
	 * 
	 * 아이템의 가격과 관련된 처리는 여기에 없습니다.
	 * 
	 * @param item 장착하려는 아이템
	 */
	public void install(Item item) {
		
		Gdx.app.log(Tyrian.LOG, "Installing item : " + item);
		
		if (item instanceof ShipModel) {
			this.shipModel = (ShipModel)item;
		} else if (item instanceof FrontGun) {
			this.frontGun = (FrontGun)item;
		} else if (item instanceof Shield) {
			this.shield = (Shield)item;
		} else {
			throw new IllegalArgumentException("Unknown item : " + item);
		}//end if else
		
	}//end of install()


	/*----------------------------------
	 * 
	 * Serialization 구현 부분...
	 * 
	 ----------------------------------*/
	
	
	private static final String KEY_SHIP_MODEL = "shipModel";
	private static final String KEY_FRONT_GUN = "frontGun";
	private static final String KEY_SHIELD = "shield";


	@Override
	public void write(Json json) {

		json.writeValue(KEY_SHIP_MODEL, this.shipModel);
		json.writeValue(KEY_FRONT_GUN, this.frontGun);
		json.writeValue(KEY_SHIELD, this.shield);
		
	}//end of write()

	@Override
	public void read(Json json, OrderedMap<String, Object> jsonData) {
		
		this.shipModel = json.readValue(KEY_SHIP_MODEL, ShipModel.class, jsonData);
		this.frontGun = json.readValue(KEY_FRONT_GUN, FrontGun.class, jsonData);
		this.shield = json.readValue(KEY_SHIELD, Shield.class, jsonData);
		
	}//end of read()

}
//end of Ship.java